package cn.bitkit.assist.constants;

import cn.bitkit.base.constants.DialectType;
import lombok.AllArgsConstructor;

import java.util.Arrays;

/**
 * @author changbo
 */
@AllArgsConstructor
public enum MybatisColumnEnum {
    /** */
    VARCHAR("VARCHAR", "VARCHAR", 64, null, null),
    BLOB("BLOB", "BLOB", null, null, null),
    //
    DECIMAL("DECIMAL", "DECIMAL", 20, 4, DialectType.mysql),
    PG_DECIMAL("DECIMAL", "NUMERIC", 20, 4, DialectType.postgresql),
    OR_DECIMAL("DECIMAL", "DECIMAL", 20, 4, DialectType.oracle),
    //
    DATE("DATE", "DATETIME", null, 0, DialectType.mysql),
    PG_DATE("DATE", "TIMESTAMP", null, 0, DialectType.postgresql),
    OR_DATE("DATE", "DATE", null, 0, DialectType.oracle),
    //
    BIGINT("BIGINT", "BIGINT", null, null, DialectType.mysql),
    PG_BIGINT("BIGINT", "INT8", null, null, DialectType.postgresql),
    OR_BIGINT("BIGINT", "INTEGER", null, null, DialectType.oracle),
    //
    INTEGER("INTEGER", "INT", null, null, DialectType.mysql),
    PG_INTEGER("INTEGER", "INT4", null, null, DialectType.postgresql),
    OR_INTEGER("INTEGER", "INTEGER", null, null, DialectType.oracle),

    ;
    public final String mybatisType;
    public final String columnType;
    public final Integer length;
    public final Integer scale;
    public final DialectType dialectType;

    public static MybatisColumnEnum getColumnType(String mybatisType, DialectType dialectType){
        return Arrays.stream(MybatisColumnEnum.values())
                .filter(e -> e.mybatisType.equals(mybatisType)
                        && (e.dialectType == null || dialectType.equals(e.dialectType)))
                .findFirst().orElse(null);
    }

}
